package com.rs.portal.doctor.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.jaxplat.commons.Row;
import com.jaxplat.util.StringUtils;
import com.rs.commons.RsConstant;
import com.rs.commons.delegate.CommonDelegate;
import com.rs.commons.web.PortalBaseAction;
import com.rs.rsbiz.dept.delegate.SecCatalogDelegate;
import com.rs.rsbiz.doctor.delegate.DoctorDelegate;
import com.rs.rsbiz.doctor.delegate.SearchDelegate;
import com.rs.util.Constants;
import com.rs.util.Pagination;

public class IndexAction extends PortalBaseAction
{
    private DoctorDelegate doctorDelegate;

    private CommonDelegate commonDelegate;

    private SecCatalogDelegate secCatalogDelegate;

    private SearchDelegate searchDelegate;

    public CommonDelegate getCommonDelegate()
    {
        return commonDelegate;
    }

    public void setCommonDelegate(CommonDelegate commonDelegate)
    {
        this.commonDelegate = commonDelegate;
    }

    public SecCatalogDelegate getSecCatalogDelegate()
    {
        return secCatalogDelegate;
    }

    public void setSecCatalogDelegate(SecCatalogDelegate secCatalogDelegate)
    {
        this.secCatalogDelegate = secCatalogDelegate;
    }

    public DoctorDelegate getDoctorDelegate()
    {
        return doctorDelegate;
    }

    public void setDoctorDelegate(DoctorDelegate doctorDelegate)
    {
        this.doctorDelegate = doctorDelegate;
    }

    public SearchDelegate getSearchDelegate()
    {
        return searchDelegate;
    }

    public void setSearchDelegate(SearchDelegate searchDelegate)
    {
        this.searchDelegate = searchDelegate;
    }

    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row params = this.parseRequestParametersToRow(request);
        Map attrs = new HashMap();
        String cityCode = "";
        String currCityCode = RsConstant.DEFAULT_CITY_CODE;
        String currCityName = RsConstant.DEFAULT_CITY_NAME;
        if (StringUtils.isEmpty(request.getSession().getAttribute("curr_city_code")))
        {
            cityCode = RsConstant.DEFAULT_CITY_CODE;
        }
        else
        {
            cityCode = (String) request.getSession().getAttribute("curr_city_code");
        }

        Row cityInfo = this.commonDelegate.getCity(cityCode);
        if (null != cityInfo)
        {
            currCityCode = cityInfo.getString("city_code");
            currCityName = cityInfo.getString("city_name");
        }
        request.getSession().setAttribute("curr_city_code", currCityCode);
        request.getSession().setAttribute("curr_city_name", currCityName);

        List hosList = commonDelegate.getAllHospitalOfCity(cityCode);

        Row secCatalogList = this.secCatalogDelegate.getAllCatalog();

        attrs.put("hosList", hosList);
        if (null != secCatalogList)
        {
            if (null != secCatalogList.get("parent"))
            {
                attrs.put("parent_cat", secCatalogList.get("parent"));
            }
            if (null != secCatalogList.get("parent"))
            {
                attrs.put("child_cat", secCatalogList.get("child"));
            }
        }
        attrs.put("request_uri", request.getRequestURI());
        return new ModelAndView("index", attrs);
    }

    public ModelAndView search(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row params = this.parseRequestParametersToRow(request);
        if (null == params.get("skey"))
        {
            return new ModelAndView("redirect:/web2/indexAction.do?method=index");
        }
        else
        {
            params.put("pageNumber", params.getInt("page") == 0 ? 1 : params.getInt("page"));
            params.put("pageSize", params.getInt("rows") == 0 ? 10 : params.getInt("rows"));
            params.put("pageIndex", params.getInt("pageNumber"));

            Object[] result = searchDelegate.search(params);
            List rows = (List) result[0];
            Integer totalCount = (Integer) result[1];

            // 取得总记录数
            int pageIndex = 1;
            if (params.getInt("page") != 0)
            {
                pageIndex = params.getInt("page");
            }
            // 处理分页
            Pagination pagination = new Pagination();
            String pagingHtml = pagination.getGoogleToolsBar(totalCount.intValue(), "doSearchPaging", pageIndex, RsConstant.PAGESIZE);
            Map attrs = new HashMap();
            attrs.put("totalCount", totalCount.intValue());
            attrs.put("rows", rows);
            attrs.put("pagingHtml", pagingHtml);
            attrs.put("request_uri", request.getRequestURI());
            attrs.put("skey", params.getString("skey"));
            return new ModelAndView("search", attrs);
        }
    }
}
